package com.leku.video.service.util;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.binary.Base64;

import com.leku.commons.util.StringUtil;

/**
 * 
 * 功能描述:人人影视地址加密
 *
 * @author Ranphy
 * @created 2018年1月3日
 * @version 1.0.0
 */

public class EncodeUrlUtil {

    /**
     * 
     * 功能描述:针对人人影视的视频地址进行加密
     *
     * @param url
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public static String encodeRRTV(String url) {
        StringBuilder sb = new StringBuilder();
        int i = 0;
        String result = "";
        if (url.contains("hcqk") || url.contains("rrsp") || url.contains("rrtv-test")) {
            i = 1;
            String base64Url = StringUtil.encodeBase64(url);
            if (base64Url.length() > 50) {
                sb.append(base64Url.substring(0, 7 + i));
                sb.append(StringUtil.getRandomString(1));
                sb.append(base64Url.substring(7 + i, 18 + i));
                sb.append(StringUtil.getRandomString(1));
                sb.append(base64Url.substring(18 + i, 25 + i));
                sb.append(StringUtil.getRandomString(1));
                sb.append(base64Url.substring(25 + i));
                result = "SS1://" + sb.toString();
            } else if (base64Url.length() > 25) {
                sb.append(base64Url.substring(0, 7 + i));
                sb.append(StringUtil.getRandomString(1));
                sb.append(base64Url.substring(7 + i, 18 + i));
                sb.append(StringUtil.getRandomString(1));
                sb.append(base64Url.substring(28 + i, base64Url.length() - 28 - i));
                result = "SS1://" + sb.toString();
            } else if (base64Url.length() > 10) {
                sb.append(base64Url.substring(0, 7 + i));
                sb.append(StringUtil.getRandomString(1));
                sb.append(base64Url.substring(7 + i, base64Url.length() - 7 - i));
                result = "SS1://" + sb.toString();
            } else {
                result = "SS1://" + base64Url;
            }
        }else{
            result=url;
        }
        return result;
    }
    
    
    /**
     * 
     * 功能描述:针对韩剧、泰剧的视频地址进行加密
     *
     * @param url
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public static String  encodeHTTV(String url) {
        StringBuilder sb = new StringBuilder();
        int i = 0;
        String result="";
        if (url.contains("www.97taiju.com") || url.contains("kkk.hanjucc.com")) {
            i = 2;
            String base64Url = StringUtil.encodeBase64(url);
            if (base64Url.length() > 50) {
                sb.append(base64Url.substring(0, 7 + i));
                sb.append(StringUtil.getRandomString(1));
                sb.append(base64Url.substring(7 + i, 18 + i));
                sb.append(StringUtil.getRandomString(1));
                sb.append(base64Url.substring(18 + i, 25 + i));
                sb.append(StringUtil.getRandomString(1));
                sb.append(base64Url.substring(25 + i));
                result="SS2://"+sb.toString();
            } else if (base64Url.length() > 25  ) {
                sb.append(base64Url.substring(0, 7 + i));
                sb.append(StringUtil.getRandomString(1));
                sb.append(base64Url.substring(7 + i, 18 + i));
                sb.append(StringUtil.getRandomString(1));
                sb.append(base64Url.substring(28 + i, base64Url.length() - 28 - i));
                result="SS2://"+sb.toString();
            } else if (base64Url.length() > 10 ) {
                sb.append(base64Url.substring(0, 7 + i));
                sb.append(StringUtil.getRandomString(1));
                sb.append(base64Url.substring(7 + i, base64Url.length() - 7 - i));
                result="SS2://"+sb.toString();
            } else {
                result="SS2://"+base64Url;
            }
        }else{
            result=url;
        }
        return result;
    }
    
    /**
     * 
     * 功能描述:AES加密
     *
     * @param url
     * @param key
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public static String encodeDateDes(String url,String key) {

        SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
        String date = key+ df.format(new Date());
        
        String encrypt = encrypt(url, date);
        
        return encrypt;
    }
    
    /**
     * 
     * 功能描述:人人视频AES加密
     *
     * @param url
     * @param key
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public static String encodeDes(String url, String key) {
        
        String encrypt = encrypt(url, key);
        
        return encrypt;
        
    }
    /**
     * 
     * 功能描述:加密处理
     *
     * @param input
     * @param pass
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    private  static String encrypt(String input,String pass) {
        byte[] crypted = null;
        try {
            int ss= 16-input.length() % 16;
            
            for(int i=0;i<ss;i++){
                input += "\0";
            }
            
            SecretKeySpec skey = new SecretKeySpec(pass.getBytes(), "AES");
            Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding");
            cipher.init(Cipher.ENCRYPT_MODE, skey);
            crypted = cipher.doFinal(input.getBytes("UTF-8"));
        } catch (Exception e) {
           e.printStackTrace();
        }
        
        return new String(Base64.encodeBase64(crypted));
    }

    /**
     * 
     * 功能描述:针对40视频的处理
     *
     * @param url
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public static String encode40URL(String url) {
        StringBuilder sb = new StringBuilder();
        int i = 0;
        String result = "";
        if (url.contains("phone.android.download")) {
            String base64Url = StringUtil.encodeBase64(url);
            if (base64Url.length() > 50) {
                sb.append(base64Url.substring(0, 7 + i));
                sb.append(StringUtil.getRandomString(1));
                sb.append(base64Url.substring(7 + i, 18 + i));
                sb.append(StringUtil.getRandomString(1));
                sb.append(base64Url.substring(18 + i, 25 + i));
                sb.append(StringUtil.getRandomString(1));
                sb.append(base64Url.substring(25 + i));
                result = "SS://" + sb.toString();
            } else if (base64Url.length() > 25) {
                sb.append(base64Url.substring(0, 7 + i));
                sb.append(StringUtil.getRandomString(1));
                sb.append(base64Url.substring(7 + i, 18 + i));
                sb.append(StringUtil.getRandomString(1));
                sb.append(base64Url.substring(28 + i, base64Url.length() - 28 - i));
                result = "SS://" + sb.toString();
            } else if (base64Url.length() > 10) {
                sb.append(base64Url.substring(0, 7 + i));
                sb.append(StringUtil.getRandomString(1));
                sb.append(base64Url.substring(7 + i, base64Url.length() - 7 - i));
                result = "SS://" + sb.toString();
            } else {
                result = "SS://" + base64Url;
            }
        }else{
            result=url;
        }
        return result;
    }


    
}
